package tech.eyao.hs60x.user.controller;

import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import tech.eyao.hs60x.common.ReturnCode;
import tech.eyao.hs60x.dto.BaseResult;
import tech.eyao.hs60x.dto.LbsDTO;
import tech.eyao.hs60x.model.domain.LbsDO;
import tech.eyao.hs60x.service.LbsService;
import tech.eyao.hs60x.utils.TokenUtils;

@RequestMapping("/lbs")
@RestController
@Api("LBS相关")
public class LbsController {
    private static final Logger logger = LoggerFactory.getLogger(LbsController.class);
    @Autowired
    private LbsService          lbsService;

    @PostMapping("/getTrack.do")
    @ApiOperation(value = "分页查询手表LBS轨迹",response=LbsDO.class)
    public BaseResult getPageListByWatchId(@ApiParam("手表账户ID") Long watchId, 
    									   @ApiParam("id") @RequestParam(value = "id",defaultValue="0") long id,
                                           @ApiParam("起始日期") @RequestParam(value = "startDate", defaultValue="") Date startDate,
                                           @ApiParam("结束日期") @RequestParam(value = "endDate", defaultValue="") Date endDate) {
        BaseResult baseResult = new BaseResult();
        try {
        	logger.info("LbsController getPageListByWatchId watchId = {}, id = {}, startDate = {}, endDate = {}", watchId, id, startDate, endDate);
            // 判断当前用户和手表是否存在绑定关系
            List<LbsDO> lists = lbsService.getPageListByWatchId(TokenUtils.getUserId(), watchId , id, startDate, endDate);
            baseResult.data = lists;
            baseResult.code = ReturnCode.C_SUCCESS_CODE;
            baseResult.message = ReturnCode.SUCCESS_CODE.getDesc();
        } catch (Exception e) {
            logger.error("[LbsController getPageListByWatchId] error.", e);
        }
        return baseResult;
    }

    //@PostMapping("/uploadWatchLbs.do")
    //@ApiOperation(value = "上传手表端LBS信息")
   /* public BaseResult uploadWatchLbs(@ApiParam("经度") double longitude,
                                     @ApiParam(value = "纬度") double latitude,
                                     @ApiParam(value = "精度") @RequestParam(value="accuracy",defaultValue="0") int accuracy,
                                     @ApiParam(value = "定位类型") @RequestParam(value="type",defaultValue="0") int type,
                                     @ApiParam(value = "地址1") @RequestParam(value="address1",defaultValue="") String address1,
                                     @ApiParam(value = "地址2")  @RequestParam(value="address2",defaultValue="") String address2) {
        BaseResult baseResult = new BaseResult();
        try {
            if (longitude > CommonConstants.ZERO & latitude > CommonConstants.ZERO) {
                lbsService.insert(TokenUtils.getUserId(), longitude, latitude, accuracy, type, address1 ,address2);
                baseResult.code = ReturnCode.C_SUCCESS_CODE;
                baseResult.message = ReturnCode.SUCCESS_CODE.getDesc();
            }
        } catch (Exception e) {
            logger.error("[LbsController uploadWatchLbs error.]", e);
        }
        return baseResult;
    }*/
    
    @PostMapping("/getWatchLbs.do")
    @ApiOperation(value="获取手表最新LBS信息",response=LbsDTO.class)
    public BaseResult getWatchLbs(long watchId) {
    	return lbsService.getWatchLbs(watchId);
    }
}
